//
//  ViewController.m
//  自定义CollectionView布局
//
//  Created by 焦林生 on 15/11/30.
//  Copyright © 2015年 jiaolinsheng. All rights reserved.
//

#import "ViewController.h"
#import "JLSLineLayout.h"
#import "JLSImageCell.h"
#import "JLSCircleLayout.h"
#import "JLSStackLayout.h"


@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *images;

@end

@implementation ViewController

static NSString *const ID = @"image";

//
- (NSMutableArray *)images {

    if (_images == nil) {
        self.images = [[NSMutableArray alloc] init];
        for (int i = 1; i <= 20; i++) {
            [self.images addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    
    return _images;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat w = self.view.frame.size.width;
    CGRect rect = CGRectMake(0, 100, w, 300);
    
    //创建布局
    JLSStackLayout *layout = [[JLSStackLayout alloc] init];
    
    //创建collction
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
    collection.dataSource = self;
    collection.delegate = self;
    [collection registerNib:[UINib nibWithNibName:@"JLSImageCell" bundle:nil] forCellWithReuseIdentifier:ID];

    
    [self.view addSubview:collection];
    self.collectionView = collection;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    if ([self.collectionView.collectionViewLayout isKindOfClass:[JLSStackLayout class]]) {
        [self.collectionView setCollectionViewLayout:[[JLSCircleLayout alloc] init] animated:YES];
    } else if ([self.collectionView.collectionViewLayout isKindOfClass:[JLSCircleLayout class]]) {
    
        [self.collectionView setCollectionViewLayout:[[JLSStackLayout alloc] init] animated:YES];
    }
}

#pragma mark - 数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    JLSImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
   
    cell.image = self.images[indexPath.item];
    return cell;
}

#pragma mark - 代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    //删除点击cell对应的数据
    [self.images removeObjectAtIndex:indexPath.item];
    
    //刷新数据
    [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
}
@end
